package com.eulog.gwt.axisdesign.propertyA.client;

import com.eulog.gwt.euloggwtlibrary.gxtext.client.layout.HorizontalCenterLayout;
import com.eulog.gwt.euloggwtlibrary.user.client.ui.HrefLink;
import com.eulog.gwt.euloggwtlibrary.user.client.ui.musicPlayer.MusicPlayer;
import com.eulog.gwt.euloggwtlibrary.user.client.ui.musicPlayer.MusicPlayerFactory;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MasterRack extends LayoutContainer implements HistoryListener {
	final static public String homeHistoryToken = "home";
	final static public String galleryPhotoHistoryToken  = "galleryPhoto";
	final static public String propertyInfoHistoryToken = "propertyInfo";
	final static public String contactHistoryToken = "contact";

	final static public String designFolder = "axisdesign/propertydesign/";
	final static public String imageNamePreFix = designFolder+"slideshow/";
	final static public String imageNamePostFix = ".jpg";
	final static public String thumbImageNamePreFix = imageNamePreFix+"/thumb/thumb_";
	final static public String thumbImageNamePostFix = ".jpg";
	final static public String bgm = designFolder+"bgm.mp3";
	
	protected boolean singlepage;
	
	protected int imageCount;
	protected int thumbsPerPage;
	
	protected int slideImageHeight;
	
	protected int movieWidth;
	protected int movieHeight;
	
	protected String mailto;
	protected String title;
	
	public static ImageManager imageManager;
	
	protected VerticalPanel panel;
	
	protected SimplePanel topPanel;
	protected HorizontalPanel naviPanel;
	protected LayoutContainer contentPanel;
	protected SimplePanel bottomPanel;
	protected LayoutContainer copyrightPanel;
	
	protected FlashMoviePanel flashMoviePanel;
	protected SlideShow slideShow;
	
	public MasterRack(int imageCount,int thumbsPerPage,int slideImageHeight,String title,String mailto,boolean singlepage) {
		
		this.slideImageHeight = slideImageHeight;
		
		this.imageCount = imageCount;
		this.thumbsPerPage = thumbsPerPage;
		this.movieWidth = 713;
		this.movieHeight = 441;
		
		this.title = title;
		this.mailto = "mailto:"+ mailto;
		this.singlepage = singlepage;
		
		imageManager = new ImageManager();
		
		setLayout(new HorizontalCenterLayout());
		panel = new VerticalPanel();
		
		topPanel = new SimplePanel();
		naviPanel = new HorizontalPanel(); 
		contentPanel = new LayoutContainer();
		contentPanel.setAutoHeight(true);
		contentPanel.setAutoWidth(true);
		bottomPanel = new SimplePanel();
		copyrightPanel = new LayoutContainer();
		
		
	
		flashMoviePanel = new FlashMoviePanel(movieWidth,movieHeight);
		slideShow = new SlideShow(imageCount,thumbsPerPage,imageNamePreFix,imageNamePostFix,thumbImageNamePreFix,thumbImageNamePostFix);
		slideShow.getImagePanel().setHeight(slideImageHeight);
		init();
		
	}
	
	public MasterRack(
			int imageCount,
			int thumbsPerPage,
			int slideImageHeight,
			int movieWidth,
			int movieHeight,
			String title,
			String mailto,
			boolean singlepage) {
		this.slideImageHeight = slideImageHeight;
		
		this.imageCount = imageCount;
		this.thumbsPerPage = thumbsPerPage;
		this.movieWidth = movieWidth;
		this.movieHeight = movieHeight;
		
		this.title = title;
		this.mailto = "mailto:"+ mailto;
		this.singlepage = singlepage;
		
		imageManager = new ImageManager();
		
		setLayout(new HorizontalCenterLayout());
		panel = new VerticalPanel();
		
		topPanel = new SimplePanel();
		naviPanel = new HorizontalPanel(); 
		contentPanel = new LayoutContainer();
		contentPanel.setAutoHeight(true);
		contentPanel.setAutoWidth(true);
		bottomPanel = new SimplePanel();
		copyrightPanel = new LayoutContainer();
		
	
		flashMoviePanel = new FlashMoviePanel(movieWidth,movieHeight);
		slideShow = new SlideShow(imageCount,thumbsPerPage,imageNamePreFix,imageNamePostFix,thumbImageNamePreFix,thumbImageNamePostFix);
		slideShow.getImagePanel().setHeight(slideImageHeight);
		
		init();
	}
	
	protected void init() {
		add(panel);
		Window.setTitle(title);
		panel.add(topPanel);
		if(singlepage==false) panel.add(naviPanel);
		panel.add(contentPanel);
		if(singlepage==false) panel.add(bottomPanel);
		panel.add(copyrightPanel);
		
		topPanel.add(imageManager.top);
		naviPanel.add(new Navigation());
		bottomPanel.add(imageManager.bottom);
		copyrightPanel.add(imageManager.copyright);

		copyrightPanel.layout();
		layout();
		
		History.addHistoryListener(this);
		
		topPanel.setStyleName("topPanel");
		naviPanel.setStyleName("naviPanel");
		contentPanel.setStyleName("contentPanel");
		bottomPanel.setStyleName("bottomPanel");
		
		MusicPlayer musicPlayer = MusicPlayerFactory.getDefaultMusicPlayer();
		musicPlayer.addMusic(bgm);
		RootPanel.get().add(new HTML(musicPlayer.getHtml()));
	}
	
	@Override
	protected void onLoad() {
		super.onLoad();
		History.fireCurrentHistoryState(); 
	}
	
	public void onHistoryChanged(String historyToken) {
		try {
			if (historyToken == homeHistoryToken) {
				switch2home();
			}
			else if (historyToken == propertyInfoHistoryToken) {
				switch2propertyinfo();
			}
			else if (historyToken == contactHistoryToken) {
				switch2contact();
			}
			else if (historyToken.substring(0, 12).equals(galleryPhotoHistoryToken)) {
				switch2slideshow();
			}
			else {
				switch2home();
			}
		} catch (IndexOutOfBoundsException e) {
			switch2home();
		}
	}
	
	public void clearContent() {
		contentPanel.removeAll();
	}
	
	public void switch2home() {
		clearContent();
		contentPanel.add(flashMoviePanel);
		bottomPanel.setVisible(true);
		contentPanel.layout();
		layout();
	}
	
	public void switch2slideshow() {
		clearContent();
		contentPanel.add(slideShow.panel);
		slideShow.imageChange(1);
		bottomPanel.setVisible(false);
		contentPanel.layout();
		layout();
	}
	
	public void switch2propertyinfo() {
		clearContent();
		contentPanel.add(imageManager.propertyinfo);
		bottomPanel.setVisible(true);
		contentPanel.layout();
		layout();
	}
	
	public void switch2contact() {
		HrefLink.gotoLink(mailto);
		clearContent();
		contentPanel.add(imageManager.contact);
		bottomPanel.setVisible(true);
		contentPanel.layout();
		layout();
	}

}

